package com.lbb.mvvm.activity

import android.os.Bundle
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import com.lbb.mvc.activity.BaseActivity
import com.lbb.mvvm.bean.UIStatus
import com.lbb.mvvm.model.UIStatusModel

abstract class BaseActivity< B : ViewDataBinding> : BaseActivity<B>() {
   lateinit var uiStatusModel:UIStatusModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        uiStatusModel= ViewModelProvider(this, ViewModelProvider.NewInstanceFactory()).get(UIStatusModel::class.java)
        lifecycle.addObserver(uiStatusModel)
        uiStatusModel.uiStatus.observe(this, {
            @Suppress("WHEN_ENUM_CAN_BE_NULL_IN_JAVA")
            when (it) {
                UIStatus.CONTENT -> showContentView()
                UIStatus.EMPTY -> showEmptyView()
                UIStatus.ERROR -> showErrorView { onOnClickErrorViewListener() }
                UIStatus.LOADING -> showLoadingView()
            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        lifecycle.removeObserver(uiStatusModel)
    }

    open fun onOnClickErrorViewListener(){

    }
}